package gipfProject.games.zertz.gui;

import gipfProject.tools.GPLPopup;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import gipfProject.games.zertz.engine.ZertzEngine;

public class ZertzMenu extends JMenuBar{
	private ZertzUI 			gui;
	private ZertzEngine 		engine;
	private JFrame				parentFrame;

	private JDialog				gplPopup;
	private int					commandMask;
	
	
	public ZertzMenu( ZertzUI gui, ZertzEngine engine, JFrame parentFrame){
		this.gui = gui;
		this.engine = engine;
		this.parentFrame = parentFrame;
		
		gplPopup = new GPLPopup(parentFrame);
		
		createMenu();
	}
	
	//Create the standard UI menu bar.
	private void createMenu()
	{
		// Determine which CommandMask to use
		if (System.getProperty("os.name").contains("Mac OS X"))
		{
			commandMask = ActionEvent.META_MASK;
		}
		else  // use this for Windows and other non-Mac systems.
		{
			commandMask = ActionEvent.CTRL_MASK;
		}
		
		createFileMenu();
		createHelpMenu();
	}
	
	private void createFileMenu(){
		// Build the file menu
		JMenu fileMenu = new JMenu("File");
		fileMenu.setMnemonic(KeyEvent.VK_F);
				
		// Build the New Game item.
		JMenuItem newGameItem = new JMenuItem("New Game", KeyEvent.VK_N);
		newGameItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, commandMask));
		fileMenu.add(newGameItem);
		
		newGameItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				engine.newGame();
				gui.refreshDisplay();
			}
		});
		
		// Build the Resign item.
		JMenuItem resignItem = new JMenuItem("Resign");
		fileMenu.add(resignItem);
		
		resignItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				engine.resign();
				gui.refreshDisplay();
			}
		});
		
		// Build the Save Game item.
		
		// Build the Load Game item.
				
		// Build the Exit item.
		JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X);
		exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, commandMask));
		fileMenu.add(exitItem);

		exitItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				System.exit(0);
			}
		});

		// Add the file menu to the menu bar.
		this.add(fileMenu);		
	}
	
	private void createHelpMenu(){
		// Create the help menu
		JMenu helpMenu = new JMenu("Help");
		helpMenu.setMnemonic(KeyEvent.VK_H);

		// Create the "GPL Text" item.
		JMenuItem gplItem = new JMenuItem("GPL License", KeyEvent.VK_G);
		helpMenu.add(gplItem);

		gplItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				displayGPLPopup();
			}
		});
		
		// Add the help menu to the menu bar.
		this.add(helpMenu);
	}

	private void displayGPLPopup()
	{
		gplPopup.setVisible(true);
	}
	
	private void displayKeyboardPopup()
	{
		//Creates a new line and indents it
		String indent = "\n     ";
		
		//Create our pop-up text
		String text = "";
		text += "Keyboard commands for Arimaa:";
		text += "\n\n";
		text += "General Keys:";
		text += indent + "ESCAPE: Return pieceInHand to it's original spot on the board";
		text += indent + "DELETE: Return pieceInHand to the bucket during the setup phase";
		text += indent + "ENTER: End Turn";
		text += indent + "'Z': Undo Turn (can also be used during setup)";
		text += indent + "'/': Toggles between mouse and keyboard controls";
		text += "\n\n" ;
		text += "These commands only work when using keyboard controls:";
		text += indent + "ARROWS: Move selection";
		text += indent + "SPACE: Grab/Drop piece at the selected location";


		//And create a basic pop-up with that text
		JOptionPane.showMessageDialog(parentFrame,text,"Keyboard controls",JOptionPane.INFORMATION_MESSAGE);
	}
}

